package com.wxy.controller;

import com.wxy.dao.IdConnectionDao;
import com.wxy.eneity.ColumnMessage;
import com.wxy.eneity.ConnThreadLocal;
import com.wxy.eneity.IdConnection;
import com.wxy.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/table")
public class TableController {
    private final IdConnectionDao idConnectionDao;
    private final TableService tableService;
    @Autowired
    public TableController(IdConnectionDao idConnectionDao,
                           TableService tableService) {
        this.idConnectionDao = idConnectionDao;
        this.tableService = tableService;
    }
    @GetMapping("getTables")
    @ResponseBody
    public List<String> getTables() {
        int id =ConnThreadLocal.CONN.get();
        IdConnection idConnection = idConnectionDao.selectById(id);
        return tableService.getTableNames(idConnection);
    }

    @RequestMapping("/addTable/{sql}")
    @ResponseBody
    public String add(@PathVariable("sql") String sql) {
        int id =ConnThreadLocal.CONN.get();
        IdConnection idConnection = idConnectionDao.selectById(id);
        return tableService.addTable(idConnection, sql);
    }
    @RequestMapping("/deleteTable/{tableName}")
    @ResponseBody
    public String delete(@PathVariable("tableName") String tableName) {
        int id =ConnThreadLocal.CONN.get();
        IdConnection idConnection = idConnectionDao.selectById(id);
        return tableService.deleteTable(idConnection, tableName);
    }
    @GetMapping("/getTableStructure/{tableName}")
    @ResponseBody
    public List<ColumnMessage> tableStructure(@PathVariable("tableName") String tableName) {
        int id =ConnThreadLocal.CONN.get();
        IdConnection idConnection = idConnectionDao.selectById(id);
        return tableService.getColumnMessage(idConnection, tableName);
    }
    @GetMapping("/getTableData/{tableName}")
    @ResponseBody
    public List<List<Object>> getTableData(@PathVariable("tableName") String tableName, @RequestParam int page, @RequestParam int size) {
        int id =ConnThreadLocal.CONN.get();
        IdConnection idConnection = idConnectionDao.selectById(id);
        return tableService.getTableData(idConnection, tableName, page, size);
    }
}
